package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.mapper.ProductUnitMapper;
import com.spzx.product.service.ProductUnitService;
import com.spzx.product.api.domain.ProductUnit;
import org.springframework.stereotype.Service;

@Service
public class ProductUnitServiceImpl extends ServiceImpl<ProductUnitMapper, ProductUnit>
        implements ProductUnitService {

    @Override
    public void checkUnitName(ProductUnit productUnit) {
        if (productUnit.getId() == null) {
            productUnit.setCreateBy(SecurityUtils.getUsername());
        } else {
            productUnit.setUpdateBy(SecurityUtils.getUsername());
        }
        String name = productUnit.getName();
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<ProductUnit>().eq(ProductUnit::getName, name));
        if (count > 0) {
            throw new ServiceException((productUnit.getId() == null ? "新增" : "修改") + "单位名称已存在");
        }
    }
}




